namespace ConsolidatedRetail.Controls
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	using Microsoft.CommerceServer.Runtime;
	using Microsoft.CommerceServer.Runtime.Catalog;

	public abstract class catalogs : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.HyperLink HyperLink1;
		protected System.Web.UI.WebControls.Repeater Repeater1;
		public string scatalogbg;
		public string scategorybg;
		public string schildcategorybg;
		public string scatalog;
		public string scategory;
		public string schildcategory;
		public ReadOnlyStringCollection ro;

		private void Page_Load(object sender, System.EventArgs e)
		{
			if(scatalog == null)
			{
				scatalog = Request.Params["CatalogName"];
			}
			
			if(scategory == null)
			{
				scategory = Request.Params["Category"];
			}

			if(schildcategory == null)
			{
				schildcategory = Request.Params["ChildCategory"];
			}

			if(! Page.IsPostBack)
			{
				try
				{
					ro = CommerceContext.Current.GetCatalogsForUser();
				} 
				catch
				{
					ro = null;
				}

				if(ro == null)
				{
					if(CommerceContext.Current.AuthenticationInfo.IsAuthenticated())
					{
						ro = CommerceContext.Current.CatalogSystem.DefaultAuthenticatedCatalogSet;
					}
					else
					{
						ro = CommerceContext.Current.CatalogSystem.DefaultAnonymousCatalogSet;
					}
				}

				Repeater1.DataSource = ro;
				Repeater1.DataBind();

				if(scatalog != null)
				{
					GetCatagories(scatalog);
				}
			}
		}

		public void GetCatagories(string sCatalogName)
		{
			for(int i=0; i < ro.Count; i++)
			{
				Repeater CategoriesControl;

				ProductCatalog prdCatalog = CommerceContext.Current.CatalogSystem.GetCatalog(ro[i]);
				DataSet ds = prdCatalog.GetRootCategories();

				CategoriesControl = (Repeater)Repeater1.Items[i].FindControl("Repeater2");

				if(sCatalogName == ro[i])
				{
					CategoriesControl.DataSource = ds;
					CategoriesControl.DataBind();

					if(scategory != null)
					{
						for(int j=0; j < ds.Tables[0].Rows.Count; j++)
						{
							Repeater ChildCategoriesControl;

							DataSet dsChildCatagory = prdCatalog.GetCategory(scategory).GetChildCategories();

							ChildCategoriesControl = (Repeater)CategoriesControl.Items[j].FindControl("Repeater3");
							if(scategory == ds.Tables[0].Rows[j]["CategoryName"].ToString())
							{
								DataColumn dc = dsChildCatagory.Tables[0].Columns.Add("ParantCategory", System.Type.GetType("System.String"));

								foreach(DataRow dr in dsChildCatagory.Tables[0].Rows)
								{
									dr["ParantCategory"] = scategory;
								}
								
								ChildCategoriesControl.DataSource = dsChildCatagory;
								ChildCategoriesControl.DataBind();
								break;
							}
						}
					}
				}
			}
		}

		public string Encode(string sValue)
		{
			return HttpUtility.UrlEncode(sValue);
		}

		public string CatalogBG(string sValue)
		{
			if(scatalog == sValue)
			{
				if(scatalogbg == null)
				{
					scatalogbg = "#DDDDDD";
				}
				return "bgcolor=\""+scatalogbg+"\"";
			}
			else
			{
				return "";
			}
		}

		public string CategoryBG(string sValue)
		{
			if(scategory == sValue)
			{
				if(scatalogbg == null)
				{
					scatalogbg = "#DDDDDD";
				}
				return "bgcolor=\""+scatalogbg+"\"";
			}
			else
			{
				return "";
			}
		}

		public string ChildCategoryBG(string sValue)
		{
			if(schildcategory == sValue)
			{
				if(schildcategorybg == null)
				{
					schildcategorybg = "#DDDDDD";
				}
				return "bgcolor=\""+schildcategorybg+"\"";
			}
			else
			{
				return "";
			}
		}

		public string plusminus(string sValue, int sType)
		{
			switch(sType)
			{
				case 1:
					if(scatalog == sValue)
					{
						return "-";
					}
					else
					{
						return "+";
					}
				case 2:
					if(scategory == sValue)
					{
						return "-";
					}
					else
					{
						return "+";
					}
				case 3:
					if(schildcategory == sValue)
					{
						return "-";
					}
					else
					{
						return "+";
					}
				default:
				{
					return "+";
				}

			}

		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	}
}
